/**
 * @file      ac_tlm_sqrt.cpp
 * @author    Luisa Madeira Cardoso
 *
 */

//////////////////////////////////////////////////////////////////////////////
// Standard includes
// SystemC includes
// ArchC includes

#include "ac_tlm_sqrt.h"
#include <math.h>

//////////////////////////////////////////////////////////////////////////////

/// Namespace to isolate sqrt_mem from ArchC
using user::ac_tlm_sqrt;

/// Constructor
ac_tlm_sqrt::ac_tlm_sqrt( sc_module_name module_name ) :
  sc_module( module_name ),
  target_export("iport")
{
    /// Binds target_export to the sqrt_mem
    target_export( *this );
    /// Initialize sqrt_mem vector
    sqrt_mem = new uint8_t[16];
    for(int k=0;k<16;k++) sqrt_mem[k]=0;

} 

/// Destructor
ac_tlm_sqrt::~ac_tlm_sqrt() {
}

/** Internal Write
  * Note: Always write 32 bits
  * @param a is the address to write
  * @param d id the data being write
  * @returns A TLM response packet with SUCCESS
*/
ac_tlm_rsp_status ac_tlm_sqrt::writel( const uint32_t &a , const uint32_t &d )
{
  *((uint32_t *) &sqrt_mem[a - 5243052]) = *((uint32_t *) &d);
 
  return SUCCESS;
}

/** Internal Read
  * Note: Always read 32 bits
  * @param a is the address to read
  * @param d id the data that will be read
  * @returns A TLM response packet with SUCCESS and a modified d
*/
ac_tlm_rsp_status ac_tlm_sqrt::readl( const uint32_t &a , uint32_t &d )
{

  sqrtm();
  
  *((uint32_t *) &d) = *((uint32_t *) &sqrt_mem[a - 5243052]);

  return SUCCESS;
}

void ac_tlm_sqrt::sqrtm()
{
  double d1;
    d1 = *((double*)(&sqrt_mem[0]));
    *((double*)(&sqrt_mem[8])) = sqrt(d1);
}

